package com.sky.service.impl;

import com.sky.context.BaseContext;
import com.sky.dto.ShoppingCartDTO;
import com.sky.entity.Dish;
import com.sky.entity.Setmeal;
import com.sky.entity.ShoppingCart;
import com.sky.mapper.DishMapper;
import com.sky.mapper.SetmealMapper;
import com.sky.mapper.ShoppingCartMapper;
import com.sky.result.Result;
import com.sky.service.ShoppingCartService;
import org.apache.commons.collections4.CollectionUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;

@Service
public class ShoppingCartServiceImpl implements ShoppingCartService {
    @Autowired
    private ShoppingCartMapper shoppingCartMapper;
    @Autowired
    private SetmealMapper setmealMapper;
    @Autowired
    private DishMapper dishMapper;
    /**
     * 添加购物车
     * @param dto
     * @return
     */
    @Override
    public Result add(ShoppingCartDTO dto) {
        //1.把dto数据拷贝给购物车实体对象
        ShoppingCart shoppingCart = new ShoppingCart();
        BeanUtils.copyProperties(dto,shoppingCart);
        //2.设置用户ID属性的值
        shoppingCart.setUserId(BaseContext.getCurrentId());
        //3.动态条件查询购物车列表
        List<ShoppingCart> shoppingCartList =  shoppingCartMapper.list(shoppingCart);

        //4.如果该商品在购物车是否列表里
        if (CollectionUtils.isNotEmpty(shoppingCartList)){
            //4.1在列表就加1
            ShoppingCart shoppingCart1 = shoppingCartList.get(0);
            shoppingCart1.setNumber(shoppingCart1.getNumber()+1);
            shoppingCartMapper.updateNumById(shoppingCart1);
        }else{
            //4.2不在购物车里添加到购物车
            if (dto.getDishId()!=null){
                //菜品
                Long dishId = dto.getDishId();
                Dish dish = dishMapper.selectById(dishId);
                shoppingCart.setImage(dish.getImage());
                shoppingCart.setName(dish.getName());
                shoppingCart.setAmount(dish.getPrice());
            }else{
                //套餐
                Long setmealId = dto.getSetmealId();
                Setmeal setmeal = setmealMapper.selectById(setmealId);
                shoppingCart.setImage(setmeal.getImage());
                shoppingCart.setName(setmeal.getName());
                shoppingCart.setAmount(setmeal.getPrice());
            }
            shoppingCart.setNumber(1);//默认商品数量
            shoppingCart.setCreateTime(LocalDateTime.now());
            //添加到购物车
            shoppingCartMapper.insert(shoppingCart);

        }
        return Result.success();
    }

    /**
     * 查看购物车
     * @return
     */
    @Override
    public Result list() {
        //获取当前用户
        Long currentId = BaseContext.getCurrentId();
        ShoppingCart sh = ShoppingCart.builder()
                .userId(currentId)
                .build();
        List<ShoppingCart> list = shoppingCartMapper.list(sh);

        return Result.success(list);
    }

    /**
     * 清空购物车
     * @return
     */
    @Override
    public Result clean() {
        Long currentId = BaseContext.getCurrentId();
        shoppingCartMapper.clean(currentId);
        return Result.success();
    }

    /**
     * 商品减一
     * @param dto
     * @return
     */
    @Override
    public Result sub(ShoppingCartDTO dto) {
        //把数据拷贝给购物车对象
        ShoppingCart shoppingCart = new ShoppingCart();
        BeanUtils.copyProperties(dto,shoppingCart);
        shoppingCart.setUserId(BaseContext.getCurrentId());

        //1.查询商品在购物车的数量 大于一就减一，等于一就删除商品
        int number = shoppingCartMapper.queryCount(shoppingCart);
        if (number>1){
            shoppingCart.setNumber(number-1);
            shoppingCartMapper.updateNumById(shoppingCart);
        }else if (number==1) {
            shoppingCartMapper.delteBySetmalOrDishId(shoppingCart);
        }

        return Result.success();
    }
}
